// index.js
// const app = getApp()
const { envList } = require('../../envList.js');

Page({
  data: {
    rec:[
      {name:'黑眼圈专题',img:'https://6c79-lyr-4gvx88rj047e3d79-1308815870.tcb.qcloud.la/1679373335000.png?sign=63d3a48d9809e399a88304b6b455df88&t=1679373346',
      value:'测测自己的黑眼圈类型~色素型黑眼圈，这类黑眼圈看起来发黄发棕......'},
      {name:'抗老专题',img:'https://6c79-lyr-4gvx88rj047e3d79-1308815870.tcb.qcloud.la/1679372923000.png?sign=e56a49e861af71fee38e0177077ff203&t=1679372929',
      value:'0成本高效抗老！拯救跨脸！抗老根源抗老法则硬核抗老'},
      {name:'祛痘专题',img:'https://6c79-lyr-4gvx88rj047e3d79-1308815870.tcb.qcloud.la/1679373180000.png?sign=d1e23fa7b579605a7ff80c2eccdbad05&t=1679373194',
      value:'皮肤科医生的祛痘红黑榜！跟医生首选，不花冤枉钱！'},
    ],
    fun_1:[
      {kind:"detectskin",
      color:"#41aae7",
      id:"皮肤病检测",
      value:"专业API检测，可用作医学参考"},
      {kind:"facetest",
      color:"#0ee668",
      id:"面部皮肤分析",
      value:"专业API检测，结果仅供参考"}],
      fun_2:[
        {kind:"",
        color:"rgb(238, 184, 123)",
        id:"历史报告",
        value:"历史记录检测报告"},
        {kind:"",
        color:"rgb(226, 213, 99)",
        id:"敬请期待",
        value:"~~~~~~~~~"}],
    banner_v:[
      {id:'氧肤',value:'只专注你的皮肤问题',img:'https://6c79-lyr-4gvx88rj047e3d79-1308815870.tcb.qcloud.la/1679410655000.png?sign=9053e5b3a1b1ddee3b440912eb2478fa&t=1679410668'},
      {id:'氧肤',value:'只专注你的皮肤问题',img:'https://6c79-lyr-4gvx88rj047e3d79-1308815870.tcb.qcloud.la/1679410724000.png?sign=2aeabebaa8c04232736533d9bf82027c&t=1679410733'},
      {id:'氧肤',value:'只专注你的皮肤问题',img:'https://6c79-lyr-4gvx88rj047e3d79-1308815870.tcb.qcloud.la/1679410791000.png?sign=800fb1ae910dd5610525716c77a2ddd0&t=1679410802'}],
    showUploadTip: false,
    powerList: [{
      title: '云函数',
      tip: '安全、免鉴权运行业务代码',
      showItem: false,
      item: [{
        title: '获取OpenId',
        page: 'getOpenId'
      },
      //  {
      //   title: '微信支付'
      // },
       {
        title: '生成小程序码',
        page: 'getMiniProgramCode'
      },
      // {
      //   title: '发送订阅消息',
      // }
    ]
    }, {
      title: '数据库',
      tip: '安全稳定的文档型数据库',
      showItem: false,
      item: [{
        title: '创建集合',
        page: 'createCollection'
      }, {
        title: '更新记录',
        page: 'updateRecord'
      }, {
        title: '查询记录',
        page: 'selectRecord'
      }, {
        title: '聚合操作',
        page: 'sumRecord'
      }]
    }, {
      title: '云存储',
      tip: '自带CDN加速文件存储',
      showItem: false,
      item: [{
        title: '上传文件',
        page: 'uploadFile'
      }]
    }, {
      title: '云托管',
      tip: '不限语言的全托管容器服务',
      showItem: false,
      item: [{
        title: '部署服务',
        page: 'deployService'
      }]
    }],
    envList,
    selectedEnv: envList[0],
    haveCreateCollection: false
  },
  onShareAppMessage() {
    const promise = new Promise(resolve => {
      setTimeout(() => {
        resolve({
          title: '氧肤'
        })
      }, 2000)
    })
    return {
      title: '氧肤',
      path: '/pages/index/index',
      promise 
    }
  },

  facetest:function(){
    wx.navigateTo({
      url: '/pages/face-test/face-test',
    })
  },

  detectskin:function(){
    wx.navigateTo({
      url:'/pages/skintest/skintest'
    })
  },

  onClickPowerInfo(e) {
    const index = e.currentTarget.dataset.index;
    const powerList = this.data.powerList;
    powerList[index].showItem = !powerList[index].showItem;
    if (powerList[index].title === '数据库' && !this.data.haveCreateCollection) {
      this.onClickDatabase(powerList);
    } else {
      this.setData({
        powerList
      });
    }
  },

  onChangeShowEnvChoose() {
    wx.showActionSheet({
      itemList: this.data.envList.map(i => i.alias),
      success: (res) => {
        this.onChangeSelectedEnv(res.tapIndex);
      },
      fail (res) {
        console.log(res.errMsg);
      }
    });
  },

  onChangeSelectedEnv(index) {
    if (this.data.selectedEnv.envId === this.data.envList[index].envId) {
      return;
    }
    const powerList = this.data.powerList;
    powerList.forEach(i => {
      i.showItem = false;
    });
    this.setData({
      selectedEnv: this.data.envList[index],
      powerList,
      haveCreateCollection: false
    });
  },

  jumpPage(e) {
    wx.navigateTo({
      url: `/pages/${e.currentTarget.dataset.page}/index?envId=${this.data.selectedEnv.envId}`,
    });
  },

  onClickDatabase(powerList) {
    wx.showLoading({
      title: '',
    });
    wx.cloud.callFunction({
      name: 'quickstartFunctions',
      config: {
        env: this.data.selectedEnv.envId
      },
      data: {
        type: 'createCollection'
      }
    }).then((resp) => {
      if (resp.result.success) {
        this.setData({
          haveCreateCollection: true
        });
      }
      this.setData({
        powerList
      });
      wx.hideLoading();
    }).catch((e) => {
      console.log(e);
      this.setData({
        showUploadTip: true
      });
      wx.hideLoading();
    });
  }
});



